package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-11：MethodHandles#filterReturnValue的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 19:09
 */
public class MethodHandlesExample11 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample11.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-11：MethodHandles#filterReturnValue的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();

        MethodHandle mhSubstring = lookup.findVirtual(String.class, "substring", MethodType.methodType(String.class, int.class));
        MethodHandle mhUpperCase = lookup.findVirtual(String.class, "toUpperCase", MethodType.methodType(String.class));
        MethodHandle mh = MethodHandles.filterReturnValue(mhSubstring, mhUpperCase);
        String str = (String) mh.invoke("Hello World", 5);

        System.out.println(str);

    }


}
